package cn.zysheep.singleton.demo4;

/**
 * @ClassName: Singleton
 * @Description: 懒汉式 线程安全的   双重校验锁
 * @Author: lyw
 * @Date: 2022/10/17 21:52
 */
public class Singleton {
    private Singleton(){};

    private static volatile Singleton singleton;

    /**
     * synchronized 关键字加锁
     * @return
     */
    public static synchronized Singleton getInstance() {
        if (singleton == null) {
            singleton = new Singleton();
        }
        return singleton;
    }

    /**
     * 双重检查锁方式
     */
    public static Singleton getInstance2() {
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}
